package se.ramfelt.psnfriends.model;

import se.ramfelt.psnfriends.model.Friend.Presence;
import android.os.Parcel;
import android.os.Parcelable;

public class ParcelableFriend implements Parcelable {

    private final Friend friend;

    public ParcelableFriend(Friend friend) {
        this.friend = friend;
    }

    public ParcelableFriend(Parcel in) {
        friend = new Friend();
        friend.setOnlineId(in.readString());
        friend.setAvatarUrl(in.readString());
        friend.setComment(in.readString());
        friend.setCurrentGame(in.readString());
        friend.setPresence(Presence.valueOf(in.readString()));
        if (in.readInt() == 1) {
            friend.setTrophies(ParcelableTrophies.CREATOR.createFromParcel(in).getTrophies());
        }
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(friend.getOnlineId());
        dest.writeString(friend.getAvatarUrl());
        dest.writeString(friend.getComment());
        dest.writeString(friend.getCurrentGame());
        dest.writeString(friend.getPresence().toString());
        dest.writeInt((friend.getTrophies() == null ? 0 : 1 ));
        if (friend.getTrophies() != null) {
            new ParcelableTrophies(friend.getTrophies()).writeToParcel(dest, flags);
        } 
    }

    public int describeContents() {
        return 0;
    }

    public Friend getFriend() {
        return friend;
    }

    public static final Parcelable.Creator<ParcelableFriend> CREATOR = new Parcelable.Creator<ParcelableFriend>() {
        public ParcelableFriend createFromParcel(Parcel in) {
            return new ParcelableFriend(in);
        }

        public ParcelableFriend[] newArray(int size) {
            return new ParcelableFriend[size];
        }
    };
}
